using Gateway.Common.Enums;
using Gateway.Domain.EFCore.Configuration;
using Gateway.Domain.Entities;
using Gateway.Domain.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Newtonsoft.Json;

namespace Gateway.Domain.Configuration;

/// <summary>
/// 审批者 配置
/// </summary>
public class WorkFlowDesignApproveConfiguration : EntityConfiguration<WorkFlowDesignApprove>
{
    public override void Configure(EntityTypeBuilder<WorkFlowDesignApprove> builder)
    {
        builder.ToTable(t => t.HasComment("审批者 配置"));
        builder.Property(x => x.ApproveType).HasMaxLength(500)
            .HasComment("审批人员类型  1指定成员  2主管  3角色  4发起人自选  5发起人自己  7连续多级主管");


        builder.Property(x => x.NodeRole).HasMaxLength(2000)
            .HasComment("节点角色 针对审批--  节点用户-选择的是角色的话，这里就会有角色信息  {id,name}")
            .HasConversion(x => JsonConvert.SerializeObject(x),
                p => JsonConvert.DeserializeObject<List<WorkDesignNodeRoleOrUser>>(p));


        builder.Property(x => x.WorkDesignNodeRoleOrUser).HasMaxLength(2000)
            .HasComment("针对审批--  节点用户-选择的是指定成员的话，这里就会有人员信息 {id,name}")
            .HasConversion(x => JsonConvert.SerializeObject(x),
                p => JsonConvert.DeserializeObject<WorkDesignNodeRoleOrUser>(p));

        builder.Property(x => x.ExamineLevel).HasMaxLength(500)
            .HasComment("审批的主管的级别，比如 发起人的第X级主管");

        builder.Property(x => x.SelectMode).HasMaxLength(500)
            .HasComment("发起人自选 1 自选一个人  2 自选多个人");


        builder.Property(x => x.DirectorMode).HasMaxLength(500)
            .HasComment("主管审批终点模式  0 直到最上层主管  1 自定义审批终点");
        builder.Property(x => x.DirectorLevel).HasMaxLength(500)
            .HasComment("主管级别  第X级主管");
        builder.Property(x => x.TermAutoStatus).IsFixedLength()
            .HasConversion(x => x.Value, p => BooleanStatusEnum.FromValue(p))
            .HasComment("超时自动审批 状态");


        builder.Property(x => x.Term)
            .HasComment("审批期限（为 0 则不生效）");
        builder.Property(x => x.TermMode).HasMaxLength(10)
            .HasComment("审批期限超时后执行  0 自动通过   1 自动拒绝");

        builder.Property(x => x.ExamineMode).HasMaxLength(10)
            .HasComment("多人审批时审批方式  1 按顺序依次审批 2会签 (可同时审批，每个人必须审批通过) 3或签 (有一人审批通过即可)");

        builder.HasOne(x => x.Design).WithOne(p => p.Approve)
            .HasForeignKey<WorkFlowDesignApprove>(x => x.WorkFlowDesignId)
            .OnDelete(DeleteBehavior.Cascade);
        base.Configure(builder);
    }
}